/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.fxbank.netbeans.plugins.xng;

import com.fxbank.netbeans.plugins.utils.Utils;
import com.sun.source.tree.Tree;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.util.TreePath;
import java.io.File;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import javax.lang.model.element.Element;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.api.project.FileOwnerQuery;
import org.netbeans.api.project.Project;
import org.netbeans.modules.java.hints.spi.AbstractHint;
import org.netbeans.spi.editor.hints.ErrorDescription;
import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
import org.netbeans.spi.editor.hints.Fix;

/**
 *
 * @author alexey
 */
public class XNGHint extends AbstractHint {

    //This hint does not enable the IDE to fix the problem:
    private static final List<Fix> NO_FIXES = Collections.<Fix>emptyList();
    //This hint applies to method invocations:
    private static final Set<Tree.Kind> TREE_KINDS =
            EnumSet.<Tree.Kind>of(Tree.Kind.METHOD_INVOCATION);
    
    private static final String PROXY_JAVA = "Proxy.java";
    private String showMessage = "XNG Hint";

    public XNGHint() {
        super(true, true, AbstractHint.HintSeverity.WARNING);
    }

    //Specify the kind of code that the hint applies to, in this case,
    //the hint applies to method invocations:
    @Override
    public Set<Kind> getTreeKinds() {
        return TREE_KINDS;
    }

    @Override
    public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) {

        if (info != null && info.getFileObject() != null &&
                info.getFileObject().getNameExt() != null &&
                info.getFileObject().getNameExt().endsWith(PROXY_JAVA)) {
        
            Tree t = treePath.getLeaf();

            Element el = info.getTrees().getElement(treePath);
            String name = el.getSimpleName().toString();
            
            if (name.equals("getMethod")) {
                Project project = FileOwnerQuery.getOwner(info.getFileObject());
                File factoryXML = new File(Utils.getFactoryByFile(project.getProjectDirectory().getPath()+File.separator+"build.xml"));
                String callingLine = t.toString();
                
                String factoryMethodName = Utils.getFactoryMethodName(callingLine);
                System.out.println("name="+factoryMethodName);
                

                String message = "";
                String[] xngs = Utils.getXNGFiles(factoryXML, factoryMethodName);

                for (String string : xngs) {
                    message += string+("\r\n");
                }
                return Collections.<ErrorDescription>singletonList(
                        ErrorDescriptionFactory.createErrorDescription(
                        getSeverity().toEditorSeverity(),
                        message,
                        NO_FIXES,
                        info.getFileObject(),
                        (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), t),
                        (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), t)));
            }
        
        }

        return null;

    }

    //This is called if/when the hint processing is cancelled:
    @Override
    public void cancel() {
    }

    //Message that the user sees in the left sidebar:
    @Override
    public String getDisplayName() {
        return showMessage;
    }

    //Name of the hint in the Options window:
    @Override
    public String getId() {
        return "XNG Hint";
    }

    //Description of the hint in the Options window:
    @Override
    public String getDescription() {
        return "This hint works only in *Proxy.java files";
    }
}
